<?php
/**
 * Created by JetBrains PhpStorm.
 * File: CategoryAction.class.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-13
 * Time: 下午8:00
 */

	class CategoryAction extends CommonAction{

		//指向分类列表
		function index(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				$category = D('Category');
				import("ORG.Util.page");
				if(isset($_POST['search'])){
					if(empty($_POST['key'])){
						$this->error('不能为空');
					}else{
						$datas = array();
						switch($_POST['search']){
							case 1 :
								$datas['id'] = $_POST['key'];
								break;
							case 2:
								$datas['name'] = array("like","%{$_POST['key']}%");
								break;
						}
						$data['sectionid'] = $_GET['sid'];
						$count = $category->where($datas)->count('id');
						if($count<1){
							$this->error('没有该分类数据');
						}elseif($count<C('PAGENUM')){
							$pagedata = array(1);
						}else{
							$pagedata = array(0,1,2,3,4,5,7,8);
						}
						$list = $category->where($datas)->order('id desc')->select();
						$page = new page($count, C('PAGENUM'));
						$show = $page->fpage($pagedata);
					}
				}else{
					$count = $category->where('sectionid='.$_GET['sid'])->count();
					if($count<1){
						$this->assign('isshow', 'no');
						$this->assign('nobody', 'no');
					}elseif($count<C('PAGENUM')){
							$pagedata = array(1);
					}else{
							$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));
					$show = $page->fpage($pagedata);
					$list = $category->relation(true)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->where('sectionid='.$_GET['sid'])->select();
				//	dump($list);
				}
				$this->assign('show', $show);
				$this->assign('list', $list);
				$this->display();
			}else{
				$this->error('异常操作');
			}
		}

		//指向添加分类页面
		function add(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				$this->display('add');
			}else{
				$this->error('异常操作');
			}

		}

		//添加分类
		function insert(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				$category = D('Category');
				if($category->create()){
					if($category->add()){
						$this->success('添加分类成功', __APP__.'/Category/index/sid/'.$_GET['sid']);
					}else{
						$this->error('添加分类失败');
					}
				}else{
					$this->error($category->getError());
				}
			}else{
				$this->error('异常操作');
			}

		}

		//删除单元
		function del(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				if(isset($_POST['del']) && !empty($_POST['del'])){
					$category = D('Category');
					foreach($_POST['del'] as $key=>$value){
						$categoryname = $category->where('id='.$value)->getField('name');
						$datas['sectionid'] = $_GET['sid'];
						$datas['id'] = $value;
						$list = $category->relation(true)->where($datas)->select();
					//	dump($list);
					//	dump(count($list[0]['article']));
						if(count($list[0]['article'])>0){
							$this->error('[ '.$categoryname.' ]分类下还有文章, 请先清空');
						}else{
							$num = $category->relation(true)->delete($value);
						}
					}
					if($num>0){
						$this->success('删除分类成功', __APP__.'/Category/index/sid/'.$_GET['sid']);
					}else{
						$this->error('删除分类失败');
					}
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}
		}

		//指向修改分类
		function edit(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				if($_GET['cid'] && !empty($_GET['cid'])){
					$category = M('Category');
					$list = $category->find($_GET['cid']);
					$this->assign('list', $list);
					$this->display();
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}

		}

		//修改分类
		function update(){
			if(isset($_GET['sid']) && !empty($_GET['sid'])){
				if(isset($_GET['cid']) && !empty($_GET['cid'])){
					if(isset($_POST['id']) && !empty($_POST['id'])){
						$category = D('Category');
						if($category->create()){
							if($category->save()){
								$this->success('修改分类成功', __APP__.'/Category/index/sid/'.$_GET['sid']);
							}else{
								$this->error('修改分类失败');
							}
						}else{
							$this->error($category->getError());
						}
					}else{
						$this->error('异常操作');
					}
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}
		}


		function select(){

			$category = D('Category');
			import("ORG.Util.page");

			if(isset($_POST['search'])){
				if(empty($_POST['key'])){
					$this->error('不能为空');
				}else{
					$datas = array();
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
					}

					if(isset($_SESSION['mid']) && !empty($_SESSION['mid'])){
						if(isset($_SESSION['cid']) && !empty($_SESSION['cid'])){

							$list = $category->where($datas)->order('id desc')->select();
							$count = $category->where($datas)->count('id');
							if($count<1){
								//	$this->assign('isshow', 'no');
								$this->error('没有该分类数据');
							}elseif($count<C('PAGENUM')){
								$pagedata = array(1);
							}else{
								$pagedata = array(0,1,2,3,4,5,7,8);
							}
							$page = new page($count, C('PAGENUM'));

							$show = $page->fpage($pagedata);
							$this->assign('isshow', 'yes');
							$this->assign('nobody', 'yes');

						}else{
							$this->error('异常操作');
						}
					}else{
						$this->error('异常操作');
					}


				}

			}else{

				if(isset($_GET['mid']) && !empty($_GET['mid'])){
					if(isset($_GET['cid']) && !empty($_GET['cid'])){
						$_SESSION['mid'] = $_GET['mid'];
						$_SESSION['cid'] = $_GET['cid'];
						$count = $category->count();
						if($count<C('PAGENUM')){
							$pagedata = array(1);
						}else{
							$pagedata = array(0,1,2,3,4,5,7,8);
						}
						$page = new page($count, C('PAGENUM'));
						$show = $page->fpage($pagedata);
						$list = $category->relation(true)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
						//	dump($list);
						if($count==0){
							$this->assign('isshow', 'no');
							$this->assign('nobody', 'no');
						}else{
							$this->assign('isshow', 'yes');
							$this->assign('nobody', 'yes');
						}
					}else{
						$this->error('异常操作');
					}
				}else{
					$this->error('异常操作');
				}


			}

			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();

		}


	}